layui.use(['layer', 'form', 'admin', 'laydate', 'HttpRequest', 'tree','table','func'], function () {
    var $ = layui.jquery;
    var HttpRequest = layui.HttpRequest;
    var tree = layui.tree;
    var table = layui.table;
    var func = layui.func;

    let studentInfoTable = {
        id: 'StudentTable',
        lefttree: null,
        renderQuery:function(obj){
            let query = {};
            if(obj.data.id.toString().length==12){
                query['areaCode']=selObj.data.id;
                query['orgId']='';
            }else{
                query['areaCode']='';
                query['orgId']=selObj.data.id;
            }
            return query;
        },
        reload: function (queryData) {
            table.reload(studentInfoTable.id, {
                where: queryData,
                page: {curr: 1}
            });
        }
    };



    table.render({
        url: Feng.ctxPath + '/student/page',
        elem: '#'+studentInfoTable.id,
        data: [],
        height: 'full-100',
        page: true,
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        //toolbar: '#organizationUserTbToolBar',
        cellMinWidth: 80,
        autoSort: false,
        cols: [[
            {type: 'numbers'},
            {field: 'name', align: "left", sort: true, title: '幼儿姓名', minWidth: 120},
            {field: 'studentCode', align: "left", sort: true, title: '身份证号', minWidth: 120},
            {
                field: 'sex', align: "center", sort: true, title: '性别', minWidth: 120, templet: function (data) {
                    if (data.sex === 'M') {
                        return '<span class="layui-badge layui-badge-blue">男</span>';
                    } else {
                        return '<span class="layui-badge layui-badge-red">女</span>';
                    }
                }
            },
            {field: 'orgName', align: "left", sort: true, title: '幼儿园', minWidth: 120}
        ]],
        done: function () {
        },
        parseData: Feng.parseData,
        autoSort: false
    });


    // 初始化组织树

    function renderTree() {
        new HttpRequest(Feng.ctxPath + "/kindergarten/tree", 'get', function (res) {
            tree.render({
                elem: '#organizationTree',
                onlyIconControl: true,
                data: res.data,
                click: function (obj) {
                    selObj = obj;
                    $('#organizationTree').find('.ew-tree-click').removeClass('ew-tree-click');
                    $(obj.elem).children('.layui-tree-entry').addClass('ew-tree-click');
                    studentInfoTable.reload(studentInfoTable.renderQuery(obj));
                    /*insTb.reload({
                        where: {orgId: obj.data.id},
                        page: {curr: 1},
                        url: Feng.ctxPath + '/sysUser/page',
                    });*/
                }
            });
            $('#organizationTree').find('.layui-tree-entry:first>.layui-tree-main>.layui-tree-txt').trigger('click');
        }).start();
    }
    renderTree();
    $("#btnSearch").click(function () {
        var queryData = {};
        queryData['name'] = $("#studentName").val();
        queryData['studentCode'] = $("#studentCode").val();
        studentInfoTable.reload(queryData);
    })

    $('#btnAdd').click(function () {
        if ("1339554696976781408" == selObj.data.id) {
            Feng.error("请选择一个幼儿园!");
            return;
        }
        func.open({
            title: '添加学生',
            content: Feng.ctxPath + '/view/student/add?orgId=' + selObj.data.id,
            tableId: studentInfoTable.tableId,
            endCallback: function () {
                renderTree();
            }
        });
    });

    studentInfoTable.openDetailDlg = function (data) {
        func.open({
            title: '学生详细信息',
            content: Feng.ctxPath + '/view/student/detail?studentId=' + data.id,
            tableId: studentInfoTable.id,
            endCallback: function () {
                table.reload(Student.tableId);
            }
        });
    };



    table.on('row(student-table)' ,function(obj){
        studentInfoTable.openDetailDlg(obj.data);
        //标注选中样式
        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click')
    });

    table.on('sort(student-table)', function (obj) {
        table.reload(studentInfoTable.id, {
            initSort: obj //记录初始排序
            , where: {
                sortBy: obj.field //排序字段
                , orderBy: obj.type //排序方式
            }
        });
    });

    /*// 初始化职位
    new HttpRequest(Feng.ctxPath + "/hrPosition/list", 'get', function (data) {
        positionXmSel = xmSelect.render({
            el: '#position',
            radio: true,
            clickClose: true,
            data: data.data
        });
    }).start();

    // 渲染时间选择框
    laydate.render({
        elem: '#birthday'
    });

    // 添加表单验证方法
    form.verify({
        psw: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repsw: function (value) {
            if (value !== $('#userForm input[name=password]').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        // 获取机构id
        data.field.orgId = organizationXmSel.getValue('valueStr');
        // 职位id
        data.field.positionId = positionXmSel.getValue('valueStr');

        var request = new HttpRequest(Feng.ctxPath + "/sysUser/add", 'post', function (data) {
            admin.closeThisDialog();
            Feng.success("添加成功！");
            admin.putTempData('formOk', true);
        }, function (data) {
            Feng.error("添加失败！" + data.message);
        });
        request.set(data.field);
        request.start(true);
        return false;
    });*/
});
